package com.loongtech.bi.action.bireport;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.pay.EntityNewUserLTV;
import com.loongtech.bi.manager.pay.NewUserLTVManager;
import com.loongtech.bi.manager.report.GameReportManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Helper;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 运营报表-渠道累计付费Action
 *
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/bi/channelPayReport")
public class ChannelPayReportBiAction extends BaseBiSearchAction {
    private String dateRadio = "7";  // 切换的天数：7=近7天。15=近15天。30=近30天，0=自选时间
    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY));  // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 自选时间：结束时间

    private List<GameReportManager.ChannelPayData> channelPayDatas;

    @Autowired
    NewUserLTVManager newUserLTVManager;
    @Autowired
    GameReportManager gameReportManager;

    @Autowired
    private SysLogManager sysLogManager;

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            channelPayDatas = new ArrayList<>();

            Date startDay = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            Date endDay = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            List<EntityNewUserLTV> list13 = newUserLTVManager.getChannelNewUserLTVByDate(startDay, endDay);
            if (!Helper.isEmptyValue(list13)) {
                Map<String, GameReportManager.ChannelPayData> tmpMap = new LinkedHashMap<>();

                for (EntityNewUserLTV o : list13) {
                    int d = Time.getInterval(Time.formatDate(o.getStartday(), "yyyy-MM-dd"), Time.formatDate(o.getEndday(), "yyyy-MM-dd")) + 1;
                    if (d <= 90) {
                        String key = o.getKey();
                        GameReportManager.ChannelPayData tmp = tmpMap.get(key);
                        if (tmp == null) {
                            tmp = gameReportManager.new ChannelPayData();
                            tmp.setDateStr(o.getStartday());
                            tmp.setPlatformId(o.getPlatformid());
                            tmp.setChannelId(o.getChannelid());
                            tmpMap.put(key, tmp);
                        }

                        tmp.setIncome(d, UIHelper.getYuan(o.getCashadd()));
                    }
                }

                channelPayDatas.addAll(new ArrayList<>(tmpMap.values()));
                Collections.sort(channelPayDatas, gameReportManager.new ChannelPayData().comparatorDate2);
            }

            result.put("res", "0");
            result.put("channelPayDatas", channelPayDatas);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @Override
    public void search() {
    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }

    /**
     * 切换天数
     */
    public void radioChange() {
        int stayDay = "0".equals(dateRadio) ? 2 : Integer.parseInt(dateRadio);
        dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - stayDay * Time.DAY));

        search();
    }

    /**
     * 划动天数
     */
    public void spinnerChange() {
        search();
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public List<GameReportManager.ChannelPayData> getChannelPayDatas() {
        return channelPayDatas;
    }

    public void setChannelPayDatas(List<GameReportManager.ChannelPayData> channelPayDatas) {
        this.channelPayDatas = channelPayDatas;
    }
}
